package com.roadmap.entities.ext;

import java.util.HashMap;
import java.util.Map;

import com.roadmap.biz.message.NoticeManager;
import com.roadmap.entities.Achievement;
import com.roadmap.entities.Article;
import com.roadmap.entities.Aspirant;
import com.roadmap.entities.Career;
import com.roadmap.entities.Execution;
import com.roadmap.entities.LifeGoal;
import com.roadmap.entities.LifeMap;
import com.roadmap.entities.LifePlan;
import com.roadmap.entities.LifeTask;
import com.roadmap.entities.Notice;
import com.roadmap.entities.Student;
import com.roadmap.struts.action.ActionConst;

public class NoticeExt extends Notice {
	private String dispatcherName;
	private String recipientName;
	private String actionURL;

	// each BaseEntity class has its corresponding action. e.g, LifeGoal has
	// "goal.do", LifePlan has "plan.do". This map stores entities like
	// <"LifeGoal", "goal.do">. Actually here an url will be assigned to view
	// the ahievement
	private final static Map<String, String> viewURLMap = new HashMap<String, String>();
	// which anchor parameter should be added to the URL
	private final static Map<String, String> anchorParamMap = new HashMap<String, String>();
	
	static {
		// user information, achievement, activity will be shown on the
		// same page which is handled by aspirant.do
		viewURLMap.put(Aspirant.class.getSimpleName(), "aspirant.do?");
		viewURLMap.put(Achievement.class.getSimpleName(), "aspirant.do?");
		viewURLMap.put(Student.class.getSimpleName(), "aspirant.do?");
		viewURLMap.put(Career.class.getSimpleName(), "aspirant.do?");

		viewURLMap.put(LifeMap.class.getSimpleName(), "vision.do?act=view&");
		viewURLMap.put(LifeGoal.class.getSimpleName(), "goal.do?act=view&");
		viewURLMap.put(LifePlan.class.getSimpleName(), "plan.do?act=view&");
		viewURLMap.put(LifeTask.class.getSimpleName(), "task.do?act=view&");
		viewURLMap.put(Execution.class.getSimpleName(), "exec.do?act=view&");
		viewURLMap.put(Article.class.getSimpleName(), "blog.do?act=view&");

		anchorParamMap.put(Aspirant.class.getSimpleName(), "infor");
		anchorParamMap.put(Achievement.class.getSimpleName(), "score");
		anchorParamMap.put(Student.class.getSimpleName(), "infor");
		anchorParamMap.put(Career.class.getSimpleName(), "infor");
	}

	public NoticeExt(Notice notice) {
		this.copyValues(notice);
		this.actionURL = createActionURL(notice);
	}

	private String createActionURL(Notice notice) {
		StringBuffer url = new StringBuffer();
		String view = viewURLMap.get(notice.getEntryType());
		if (view == null) {
			return null;
		}
		url.append(view);
		url.append(ActionConst.Aspirant_Id_In_Request);
		// entry needs to be notified belongs to the recipient firstly
		if (notice.getRecipientId() != null) {
			url.append("=" + notice.getRecipientId());
		}
		else {
			url.append("=" + notice.getDispatcherId());
		}
		url.append("&" + ActionConst.Item_Id_In_Request);
		url.append("=" + notice.getEntryId());
		String anchor = anchorParamMap.get(notice.getEntryType());
		if (anchor != null) {
			url.append(ActionConst.Anchor_Parameter_Flag + anchor);
		}
		return url.toString();
	}

	public String getDispatcherName() {
		return dispatcherName;
	}

	public void setDispatcherName(String dispatcherName) {
		this.dispatcherName = dispatcherName;
	}

	public String getRecipientName() {
		return recipientName;
	}

	public void setRecipientName(String recipientName) {
		this.recipientName = recipientName;
	}

	public String getActionURL() {
		return actionURL;
	}

	public void setActionURL(String actionURL) {
		this.actionURL = actionURL;
	}
}
